/**
 * 新增送修记录
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/',upload.array(), function(req, res, next) {
    let data = {
        isSuccess: false,
        data: null,
        msg: '送修记录录入失败！'
    };
    let equipmentid = _.trim(String(req.body.equipmentid));
    let state = _.trim(String(req.body.state));
    let surveyor = _.trim(String(req.body.surveyor));
    let recorderid = _.trim(String(req.body.recorderid));
    let recorddate = new Date().toLocaleDateString();
    let thecontact = _.trim(String(req.body.thecontact));
    let contact = _.trim(String(req.body.contact));
    let desc = _.trim(String(req.body.desc));

    let tableName = 'equipment_maintenance';
    let colName = "`equipmentid`,`state`,`surveyor`,`recorddate`,`recorderid`,`thecontact`,`contact`, `desc`";
    let colValue = "'" + equipmentid + "','" + state + "','" + surveyor
     + "','" + recorddate + "','" + recorderid + "','" + thecontact
     + "','" + contact + "','" + desc + "'" ;
    console.log("运行到这里")
    db.insert(tableName, colName, colValue, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            console.log(rows.affectedRows)
            if(rows.affectedRows > 0 ) {
                db.changeState(equipmentid, '3', function(err, rows) {
                    if(err) {
                        return res.json(data);
                    } else {
                        if(rows.affectedRows > 0 ) {
                            data.isSuccess = true;
                            data.msg = "送修记录录入成功！";
                            return res.json(data);
                        }
                    }
                });
            } else {
                return res.json(data);
            }
        }
    })
})

module.exports = router;